package com.app.search;

import java.util.ArrayList;

import com.core.indexer.SearchCore;

public class Search {

	private static String type;
	private static String queryLabel;
	private static int numResults;
	private static String lexiconFile;
	private static String invertedListFile;
	private static String mapFile;
	private static String stopList;
	private static ArrayList<String> queryTerms;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		// Converting Input string to args for testing
		//args = "search lexicons invlist Map information retrieval is interesting...or is it".split(" ");
		
		if(!checkcommand(args))
		{
			print_usage();
			return;
		}
				
		SearchCore search = new SearchCore();
		search.DoSearch(type, queryLabel, numResults, lexiconFile, invertedListFile, mapFile, stopList, queryTerms);

	}

	private static void print_usage()
	{
		System.out.println("USAGE: search -BM25 -q <query-label> -n <num-results> <lexicon> <invlists> <map> [-s <stoplist>] <queryterm 1> [... <queryterm N>]");
	}
	
	private static boolean checkcommand(String[] args)
	{
		if(args.length < 9)
			return false;
		
		if(args[0].compareToIgnoreCase("-BM25") != 0)
		{
			System.out.println("[%s] Unsupported similarity function!");
			return false;
		}
		type = args[0];
		
		if(args[1].compareToIgnoreCase("-q") != 0)
			return false;
		queryLabel = args[2];
		
		if(args[3].compareToIgnoreCase("-n") != 0)
			return false;
		
		try{
			numResults = Integer.valueOf(args[4]);
		}
		catch(NumberFormatException e){
			return false;
		}
		
		lexiconFile = args[5];
		invertedListFile = args[6];
		mapFile = args[7];
		
		int queryOffset = 0;
		if(args[8].compareToIgnoreCase("-s") == 0)
		{
			stopList = args[9];
			queryOffset = 10;
		}
		else
		{
			stopList = "";
			queryOffset = 8;
		}
		
		queryTerms = new ArrayList<String>();
		for(int i = queryOffset; i < args.length ;i++)
		{
			String[] strSplit = args[i].split("-");
			
			for(String query : strSplit)
			{
				queryTerms.add(query.toLowerCase());
			}
		}
		return true;
	}
}
